package com.lagou.client;

import com.lagou.mmclass.ClientScanner;
import com.lagou.mmclass.User;
import com.lagou.mmclass.UserMessage;

import java.io.IOException;

public class StuentManagerView {
    /**
     *
     * 合成复用原则
     */
    private ClientInit cic;
    public StuentManagerView(ClientInit cic) {
        this.cic = cic;
    }

    public void Manager() throws IOException, ClassNotFoundException {
        while (true){
            System.out.println("     \n\n\t管理员系统");
            System.out.println("----------------------------");
            System.out.print("  [1]学员管理模块");
            System.out.println("  [2]考题管理模块");
            System.out.println("----------------------------");
            System.out.println("请选择要进行的业务编号:");
            int choose= ClientScanner.getScanner().nextInt();
            switch (choose){
                case 1:
                    System.out.println("进入学员管理模块成功");
                    StudentManager();
                    break;
                case 2:
                    System.out.println("进入考题管理模块成功");

                    break;
                default:
                    System.out.println("输入有误，请重新输入!");
            }
        }
    }

    public void StudentManager() throws IOException, ClassNotFoundException {
//        ms.reTest();
        while (true){
            System.out.println("     \n\n\t学员管理模块");
            System.out.println("----------------------------");
            System.out.print("  [1]增加学员");
            System.out.println("  [2]删除学员");

            System.out.print("  [3]修改学员");
            System.out.println("  [4]查询学员");
            System.out.println("[0]返回上一步");
            System.out.println("-----------------------------");
            System.out.println("请输入你要进行的业务操作");
            int choose = ClientScanner.getScanner().nextInt();
            switch (choose){
                case 1:
                    clientAddUser();
                    break;
                case 2:
                    clientDeleteUser();
                    break;
                case 3:
                    clientModifyUser();
                    break;
                case 4:
                    clientFindUser();
                    break;
                case 0:
                    System.out.println("你已进入管理员系统");
                    return;
                default:
                    System.out.println("输入如有误,请重新输入");
            }
        }

    }

    private void clientFindUser() throws IOException, ClassNotFoundException {
        System.out.println("请输入你要查询的学员账户名称");
        String strName = ClientScanner.getScanner().next();
        UserMessage tum = new UserMessage("clientFindUser", new User(strName, "11111"));
        cic.getOos().writeObject(tum);
        System.out.println("客户端发送查询学员信息成功");
        tum = (UserMessage) cic.getOis().readObject();
        if("success".equals(tum.getType())){
            System.out.println("查找学员账户成功");
            System.out.println("查到的学员信息"+tum.getUser());
        }else {
            System.out.println("查找学员失败，无该学员信息");
        }
    }

    private void clientModifyUser() throws IOException, ClassNotFoundException {
        System.out.println("请你输入你要修改的学员账户名称");
        String name = ClientScanner.getScanner().next();
        System.out.println("请输入你要修改的学员密码");
        String password=ClientScanner.getScanner().next();
        UserMessage tum = new UserMessage("clientModifyUser", new User(name, password));
        cic.getOos().writeObject(tum);
        System.out.println("客户端发送的修改学员账户成功");
        tum = (UserMessage) cic.getOis().readObject();
        if("success".equals(tum.getType())){
            System.out.println("修改学员账户成功");
            System.out.println("修改后的学员信息"+tum.getUser());
        }else {
            System.out.println("修改学员失败，无该学员信息");
        }

    }

    private void clientDeleteUser() throws IOException, ClassNotFoundException {
        System.out.println("请你输入你要删除的学员账户名称");
        String strName = ClientScanner.getScanner().next();
        UserMessage tum = new UserMessage("clientDeleteUser", new User(strName, "11111"));
        cic.getOos().writeObject(tum);
        System.out.println("客户端发送的删除学员账户成功");
        tum = (UserMessage) cic.getOis().readObject();
        if("success".equals(tum.getType())){
            System.out.println("删除学员账户成功");
        }else {
            System.out.println("删除学员失败");
        }
    }

    private void clientAddUser() throws IOException, ClassNotFoundException {
        System.out.println("请输入增加的学员账户名称");
        String strName = ClientScanner.getScanner().next();
        UserMessage tum = new UserMessage("clientAddUser", new User(strName, "11111"));
        cic.getOos().writeObject(tum);
        System.out.println("客户发送的增加账户信息成功！");
        // 接收服务器发来的处理结果并给出提示
        tum = (UserMessage) cic.getOis().readObject();
        if("success".equals(tum.getType())) {
            System.out.println("增加学员账户成功！");
        } else {
            System.out.println("增加学员账户失败！");
        }

    }

}
